package com.ruoyi.thoroughfare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.thoroughfare.domain.bo.AppShareBo;
import com.ruoyi.thoroughfare.domain.vo.AppShareVo;
import com.ruoyi.thoroughfare.domain.AppShare;
import com.ruoyi.thoroughfare.mapper.AppShareMapper;
import com.ruoyi.thoroughfare.service.IAppShareService;

import java.util.List;
import java.util.Collection;

/**
 * 邀请管理Service业务层处理
 *
 * @author Zach
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
public class AppShareServiceImpl implements IAppShareService {

    private final AppShareMapper baseMapper;

    /**
     * 查询邀请管理
     */
    @Override
    public AppShareVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询邀请管理列表
     */
    @Override
    public TableDataInfo<AppShareVo> queryPageList(AppShareBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppShare> lqw = buildQueryWrapper(bo);
        Page<AppShareVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邀请管理列表
     */
    @Override
    public List<AppShareVo> queryList(AppShareBo bo) {
        LambdaQueryWrapper<AppShare> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppShare> buildQueryWrapper(AppShareBo bo) {
        LambdaQueryWrapper<AppShare> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), AppShare::getCode, bo.getCode());
        lqw.eq(bo.getUserId() != null, AppShare::getUserId, bo.getUserId());
        lqw.eq(bo.getFailureTime() != null, AppShare::getFailureTime, bo.getFailureTime());
        lqw.eq(bo.getStatus() != null, AppShare::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增邀请管理
     */
    @Override
    public Boolean insertByBo(AppShareBo bo) {
        AppShare add = BeanUtil.toBean(bo, AppShare.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邀请管理
     */
    @Override
    public Boolean updateByBo(AppShareBo bo) {
        AppShare update = BeanUtil.toBean(bo, AppShare.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppShare entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邀请管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public AppShareVo queryByCode(String yqm) {
        return baseMapper.queryByCode(yqm);
    }
}
